{% if section.settings.collection_id.id %}
{% get_collection collections_id={ section.settings.collection_id.id } %}
{% assign collection = collection  %}
{% endif %}
{{products}}

<div id="block_collection_waterfall-{{ block_id | default : section.block_id  }}" class="container_wrapper block_collection_waterfall {% if section.settings.is_fill %}full_container_wrapper{% endif %} animation-init" data-animation="animation-move-bottom">
  {% if section.settings.title != '' or section.settings.detail != '' %}
  <div class="block_title">
      {% if section.settings.title != '' %}<h2>{{ section.settings.title }}</h2>{% endif %}
      {% if section.settings.detail != '' %}<div class="block_detail">{{ section.settings.detail|html_content_filter }}</div>{% endif %}
  </div>
  {% endif %}
  <div class="mo-waterfall-warp block_collection_product {% if section.settings.is_mobile_name %}show-mobile_name{% endif %}" id='block_product_waterfall-{{ block_id | default : section.block_id  }}'>
  </div>
</div>
  {% include 'collection_product_waterfall' %}
<script type="text/javascript">
(function(){const waterfall = new waterfalls({
    id: "#block_product_waterfall-{{ block_id | default : section.block_id  }}",
    bottomDistance: 30,
    scrollBottom: scrollBottom,
    renderType:"liquid",
    renderComplete(node){
        node.find(".product_waterfall_view").click(quickview);
        window.addLazyImages && addLazyImages(node.find("img[data-src]"));
    },
    resizeConfig: [{
            min:0,
            max: 767,
            rowGutter: '15px',
            columnsGutter: '15px',
            columns: {{ section.settings.mobile_column }}
        },
        {
                min: 768,
                max: 1199,
                rowGutter: '30px',
                columnsGutter: '30px',
                columns: {{ section.settings.web_column }}
        },
        {
            min: 1200,
            rowGutter: '30px',
            columnsGutter: '30px',
            columns: {{ section.settings.web_column }}
        }
    ],
    render(data,callback) {
      engine.registerFilter('money', val => formatMoney(val, oemcart_currency))

      engine.parseAndRender($("#id-collection-product-temp").html(), {
                products: data,
                theme_config:themeConfig,
                lang:lang,
                collection_handle: "{{collection.handle }}",
                data_from:"{{data_from}}",
                collection_product_url: '{{storeConfig.collection_product_url}}'
            }).then(function (liquidHtml) {
              const node = $(liquidHtml);
              callback && callback.call(this,node)
              {% if theme_config.global.product_img_list_type != 0 %}
                renderImgList();
              {% endif %}
      });
      
    }
})

function renderImgList() {
      let productDetail = new shopProduct({})
      productDetail.renderProductImgList()
      productDetail = null
};

{% if section.settings.collection_id.id == "" %}
    waterfall.RenderDefaultData({
      image: {alt: "", src: "{{ 'empty.png' | public_asset_abs_dir_url }}"},
      inventory_policy: 1,
      inventory_quantity: 0,
      inventory_tracking: 0,
      title: "title",
      variant: {price: 18.8, compare_at_price: 188},
      variant_compare_at_price_max: 188,
      variant_compare_at_price_min: 188,
      variant_price_max: 18.8,
      variant_price_min: 18.8,
    },'{{ section.settings.button_text }}')
{% endif %}


const collection_id = '{{ section.settings.collection_id.id | default : "" }}';


let page = 1;
    let isAll = true;
    let maxRequestNumber = '{{ section.settings.count }}';
    let isScrollLoad = false;


    function getCollection() {
        waterfall.showLoad()
        if (maxRequestNumber) {
            maxRequestNumber--
        }
        moi.ajax({
            url:`/collection/{{ section.settings.collection_id.id }}/products`,
            type:"get",
            data: {
              page: page,
              pagesize: 10,
            },
            success: function(ret) {
                if (!ret.code) {
                    isScrollLoad = false;
                    waterfall.hideLoad()
                    waterfall.addStorage(ret.data.list)
                    isAll = ret.data.paginate.pageTotal === page ? true : false;

                    if (!maxRequestNumber || isAll) {
                        waterfall.showMore("{{ section.settings.button_text }}", "/collections/{{collection.handle}}")
                    }
                  
                }
            }
        })
    }

    function scrollBottom() {
        if (!isScrollLoad && !isAll && maxRequestNumber) {
            isScrollLoad = true;
            page++
            getCollection()
        }
    }
    if (collection_id) {
        getCollection();
    }
   })()
</script>
{% schema %}
{
	"tag": "",
	"class": "block_collection_waterfall",
	"icon": "icon-shangpinpubuliu",
	"is_global": false,
	"name": {
		"zh_CN": "商品瀑布",
		"en_US": "Product grid"
	},
	"max_blocks": "5",
	"settings": [
		{
			"type": "card_header",
			"label": {
				"zh_CN": "设置",
				"en_US": "Settings"
			}
		},
		{
			"type": "card_collection",
			"label": {
				"zh_CN": "选择专辑",
				"en_US": "Select collection"
			},
			"default": {
				"id": "",
				"title": ""
			},
			"id": "collection_id"
		},
		{
			"type": "card_input",
			"label": {
				"zh_CN": "标题",
				"en_US": "Title"
			},
			"id": "title",
			"default": "title"
		},
		{
			"type": "card_text_editor",
			"label": {
				"zh_CN": "简短描述",
				"en_US": "Short description"
			},
			"id": "detail",
			"default": "title"
		},
		{
			"type": "card_switch",
			"label": {
				"zh_CN": "宽度铺满",
				"en_US": "Spread the width"
			},
			"default": true,
			"id": "is_fill"
		},
		{
			"type": "card_slider",
			"label": {
				"zh_CN": "pc每排数量",
				"en_US": "PC number per row"
			},
			"default": "3",
			"id": "web_column",
			"max": "5",
			"min": "2"
		},
		{
			"type": "card_slider",
			"label": {
				"zh_CN": "移动端每排数量",
				"en_US": "Number of mobile terminals per row"
			},
			"default": "1",
			"id": "mobile_column",
			"max": "3",
			"min": "2"
		},
		{
			"type": "card_slider",
			"label": {
				"zh_CN": "加载次数",
				"en_US": "Number of loads"
			},
			"default": "3",
			"id": "count",
			"max": "5",
			"min": "1"
		},
		{
			"type": "card_input",
			"label": {
				"zh_CN": "查看更多文案",
				"en_US": "Text of view more"
			},
			"id": "button_text",
			"default": ""
		}
	],
	"blocks": [],
	"default": {
		"settings": {
			"collection_id": {
				"id": "",
				"title": ""
			},
			"title": "Collection Waterfall",
			"detail": "",
			"web_column": "4",
			"mobile_column": "2",
			"is_fill": false,
			"is_mobile_name": true,
			"count": "3",
			"button_text": "View More"
		},
		"blocks": []
	}
}
{% endschema %}